<?php
namespace Home\Model;
use Think\Model;

class AddressModel extends Model
{
	protected $_validate=array(
			['linkman','/^[a-zA-Z0-9\x{4e00}-\x{9fa5}]+$/u','收货人姓名不合规'],
			['address','/^[a-zA-Z0-9\x{4e00}-\x{9fa5}]+$/u','详细地址有特殊符号'],
			['phone','/^1(3|4|5|7|8)\d{9}$/','请输入正确的电话号码'],
			// ['code','/^[1-9][0-9]{5}$/','邮编只能是数字'],
			['email','/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/','邮箱不符合要求'],
			['email','','邮箱已经存在！',0,'unique',1 ],
			['pass','/^\w{6,20}$/','密码不符合要求！',3],
			['password','pass','密码不一致！',1,'confirm',3],
		);

	protected $_auto=[
			/*// 新增的时候把status字段设置为1
			['status','1'], 
			// 对update_time字段在更新的时候写入当前时间戳
			['addtime','time',3,'function'],*/
			//密码通过password_hash加密 
			['pass','myHash',3,'function'],
		];
	/**
	 * [address_sel 根据用户id查询数据库]
	 * @return [type] [返回用户所填过的所有地址、
	 * 统计用户所填的地址数量并返回]
	 */
	public function address_sel()
	{
		$data['uid'] = session('user')['id'];

		$map['uid'] = ['eq',$data['uid']];
		$list = $this->where($map)->select();
		$sum = count($list);
		return ['list'=>$list,'sum'=>$sum];
	}
	//=====对应收货地址栏==========================================
	/**
	 * [address_eidt 实现用户对收货地址的增加和修改功能]
	 * @return [type] [返回成功与否]
	 */
	public function address_eidt()
	{
		$data = I('post.');
		$data['address'] = $data['province'].$data['city'].$data['county'].$data['address'];

		if(!$this->create($data)){
			return $this->getError();
		}

		//根据用户对地址栏的操作查找数据库是否存在id值，如果有，则为原地址
		//如果没有则为增加新地址
		if($data['id']){
			$map['id'] = ['eq',$data['id']];
			$res = $this->where($map)->save($data);
			if($res){
				return "success";
			}else{
				return "over";
			}
		}else{
			$data['uid'] = session('user')['id'];	//为测试使用得值后期需要改成获取session值
			$res = $this->add($data);
			if($res){
				return "success";
			}else{
				return "over";
			}
		}
	}
	/**
	 * [address_status 实现用户点击默认地址的功能]
	 * @return [type] [默认地址的状态status=1则为默认值]
	 */
	public function address_status()
	{
		$data = I('get.');
		$map['id'] = ['eq',$data['id']];
		//根据用户点击的相对应的地址id值搜索数据库获得uid值
		$list = $this->where($map)->find();
		//根据uid值再搜索地址表查看该用户有多少条地址
		$m['uid'] = ['eq',$list['uid']];
		//把全部的状态改为空
		$s['status'] = "";
		$uid = $this->where($m)->save($s);
		//通过用户点击相对应的id值改变状态值，从而实现该地址的默认值
		$d['status'] = $data['status'];
		$list = $this->where($map)->save($d);
	}
	//=====对应账户安全栏=============================================
	/**
	 * [address_Safephone 实现修改手机号码功能]
	 * @return [type] [description]
	 */
	public function address_Safephone()
	{
		$user_info = M('user_info');
		$map['uid'] = session('user')['id'];
		$list = $user_info->where($map)->find();

		$data['oldphone'] = I('post.oldphone');
		$data['phone'] = I('post.phone');
		//判断手机号码是否为原号码
		if($list['phone']!=$data['oldphone']){
			return "none";
		}

		$reg = '/^1(3|4|5|7|8)\d{9}$/';
		if(!preg_match($reg, $data['phone'])){
			return "no";
		}else{
			$res = $user_info->where($map)->save($data);
			return "success";
		}
	}

	/**
	 * [address_pass 实现修改密码的功能]
	 * @return [type] [description]
	 */
	public function address_pass()
	{
		$user = M('user');
		$map['uid'] = session('user')['id'];
		$list = $user->where($map)->find();
		$data = I('post.');
		//对密码进行验证
		if(!password_verify($data['oldpass'],$list['pass'])){
			return 'none';
		}

		$reg = '/^\w{6,20}$/';
		if(!preg_match($reg, $data['pass'])){
			return 'no';
		}

		if($data['pass']!=$data['password']){
			return 'ov';
		}
		//对密码进行哈希加密
		$data['pass'] = password_hash($data['pass'],PASSWORD_DEFAULT); 
		$res = $user->where($map)->save($data);
		if($res){
			return 'success';
		}else{
			return 'over';
		}
	}

	/**
	 * [address_email 实现修改邮箱的功能]
	 * @return [type] [description]
	 */
	public function address_email()
	{
		$user = M('user');
		$map['id'] = session('user')['id'];
		$list = $user->where($map)->find();
		$data = I('post.');
		$listuser = $user->select();

		if($list['email']!=$data['oldmail']){
			return "none";
		}
		$reg = '/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/';

		if(!preg_match($reg, $data['email'])){
			return "no";
		}
		
		foreach ($listuser as $key => $value) {
			if($data['email']==$value['email']){
				return 'n';
			}
		}

		$res = $user->where($map)->save($data);
		if($res){
			return "success";
		}else{
			return "error";
		}
		
	}
	//=====对应用户信息栏==============================================
	/**
	 * [member_add 对接收到的用户输入的数据进行验证处理并写入数据库]
	 * @param  [type] $pic [接收上传的图片名]
	 * @return [type]      [返回各种标记提示错误]
	 */
	public function member_add($pic)
	{
		$user_info = M('user_info');
		$data = I('post.');
		$data['uid'] = session('user')['id'];
		$map['uid'] = ['eq',$data['uid']];
		//通过用户的id来查找user_info表的相对应的数据
		$finds = $user_info->where($map)->find();
		//判断是否有上传图片，如果有则接收图片名并改变显示图片，如果没有则取数据库的图片名
		if(empty($pic)){
			$data['picname'] = $finds['picname'];
		}else{
			$data['picname'] = $pic;
		}
		//判断是否有输入昵称
		if(empty(trim($data['name']))){
			return 'kong';
		}
		//通过用户id值查询user_info表是否存在该用户的信息，如果有则为更新操作，如果没有则为增加操作
		$bool = $user_info->where($map)->find();
		if ($bool) {
			//更新表中的数据
			$res = $user_info->where($map)->save($data);
			if($res){
				return 'success';
			}else{
				return 'over';
			}
		}else{
			//添加信息操作
			$res = $user_info->add($data);
			if($res){
				return "su";
			}else{
				return 'ov';
			}
		}
	}


	//提交订单时修改地址方法
	public function address_up(){
		$add = I('get.');
		//设置修改条件
		$map['id'] = ['eq',$add['id']];
		$res = $this->field('phone,address,code,linkman')->create($add);
		if(!$res){
			exit($this->getError());
		}
		//执行并接收返回值
		$result = $this->where($map)->save();
		return $result;
	}

	//====对应我的收藏栏========================================
	/**
	 * [Collect_show 遍历收藏表和商品表]
	 */
	public function Collect_show()
	{
		$user = M('keep');
		$map['uid'] = ['eq',session('user')['id']];
		$user_goods = M('goods');
		$list_goods = array();

		$list = $user->where($map)->select();
		foreach ($list as $key => $value) {
			$m['id'] = ['eq',$value['gid']];
			$list_goods[] = $user_goods->where($m)->find();
		}
		
		return ['list'=>$list,'list_goods'=>$list_goods];
	}
	/**
	 * [commemt_show 查看评论表]
	 * @return [type] [description]
	 */
	//==========================================
	//2017_1_25做出修改
	public function commemt_show()
	{
		$comment = M('comment');
		$goods = M('goods');
		$map['uid'] = ['eq',session('user')['id']];
		// $goods[] = array();
		$list = $comment->where($map)->order('addtime desc')->select();
		
		// foreach ($list as $key => $value) {
		// 	$m['id'] = ['eq',$value['gid']];
		// 	$list_goods[] = $goods->where($m)->find();
		// }
		$num = count($list);
		for ($i=0; $i < $num; $i++) { 
			$map = [];
			$map['id'] = $list[$i]['gid'];
			$glist = $goods->where($map)->find();
			$list[$i]['goodspic'] = $glist['pic'];
			$list[$i]['goodsname'] = $glist['goods'];
		}
		return $list;
	}
		//订单处添加新地址
	public function adds_address(){
		//获取用户id
		$uid = session('user')['id'];
		$add = I('get.');
		$add['uid'] = $uid;
		if(!$this->create($add)){
			exit($this->getError());
		}else{
			$res = $this->add($add);
			return $res;
		}
	}
	/**
	 * [order_show 查看订单表orders的数据并返回]
	 * @return [type] [description]
	 */
	public function order_show()
	{
		$uid = session('user')['id'];
		$order = M('orders');
		$map['uid'] = ['eq',$uid];
		
		$list = $order->where($map)->select();

		return $list;
	}
}